package com.sundablog.controller.index;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sundablog.pojo.AdminUser;
import com.sundablog.pojo.AdminUserRole;
import com.sundablog.result.BaseResult;
import com.sundablog.result.MenuResult;
import com.sundablog.service.index.IndexService;
import com.sundablog.utlis.RedisUtil;
import com.sundablog.utlis.SystemUtils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;

/**
 * 后台首页控制器
 * 
 * @ClassName: IndexController
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: 哒哒
 * @date: 2017年12月24日 下午5:18:50
 * 
 * @Copyright: 2017 www.sundablog.com Inc. All rights reserved.
 */
@Controller
public class IndexController {

	@Autowired
	private IndexService indexService;

	/**
	 * 显示后台登入首页 
	 * @Title: showIndex 
	 * @Description:显示后台登入首页 
	 * @param: 
	 * @return 
	 * @return: String 
	 * @throws
	 */
	@RequestMapping("/index")
	public String showIndex() {
		return "/index/index";
	}

	/**
	 * 显示后台主页登入
	 * @Title: showMain   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("/main")
	public String showMain() {
		return "/index/main";
	}

	/**
	 * 获取菜单
	 * @Title: getMenuResult   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: List<MenuResult>      
	 * @throws
	 */
	@RequestMapping("/getMenuResult")
	@ResponseBody
	public List<MenuResult> getMenuResult() {
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		String menu = RedisUtil.get("backend_Menu_"+user.getUserId());
		if ("".equals(menu) || null == menu) {
			List<MenuResult> menuResult = indexService.getMenuResult(user.getUserId());
			String jsonStr = JSONUtil.toJsonStr(menuResult);
			RedisUtil.set("backend_Menu_"+user.getUserId(), jsonStr);
			return menuResult;
		} else {
			JSONArray jsonArray = JSONUtil.parseArray(menu);
			List<MenuResult> list = jsonArray.toList(MenuResult.class);
			return list;
		}
		
		
		
	}

	/**
	 * 获取CPU 
	 * @Title: CPU   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/Cpu")
	@ResponseBody
	public BaseResult CPU() {
		SystemInfo si = new SystemInfo();
		HardwareAbstractionLayer hal = si.getHardware();
		return BaseResult.build(200, "", SystemUtils.Cpu(hal.getProcessor()));
	}

	/**
	 * 获取内存 
	 * @Title: Memory   
	 * @Description: 获取内存  
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/Memory")
	@ResponseBody
	public BaseResult Memory() {
		SystemInfo si = new SystemInfo();
		HardwareAbstractionLayer hal = si.getHardware();
		return BaseResult.build(200, "", SystemUtils.Memory(hal.getMemory()));
	}
	
	/**
	 * 获取用户头像,和用户账号
	 * @Title: selectUser   
	 * @Description: 获取用户头像,和用户账号   
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/selectUser")
	@ResponseBody
	public BaseResult selectUser() {
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		return BaseResult.build(200, "", user);
	}
	

}
